import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:ui';
import 'package:flutter/cupertino.dart';

import '../modules/mine/model/login_user_data_entity.dart';
import '../utils/sp_utils.dart';

class Global {
  /// 屏幕信息
  static MediaQueryData mediaQueryData = MediaQueryData.fromWindow(window);
  static double screenWidth = mediaQueryData.size.width;
  static double screenHeight = mediaQueryData.size.height;
  static double paddingBottom = mediaQueryData.padding.bottom;
  static double paddingTop = mediaQueryData.padding.top;
  static SPUtils? prefs;

  /// 是否是给测试打的包：如果是，将会展示测试入口，环境切换入口等
  /// 打上线包前，请务必将其改为false；
  static bool isForTestPackage = true;

  /// 是否为release版
  static bool get isRelease => const bool.fromEnvironment('dart.vm.product');

  /// APP运行的技术平台，iOS或者安卓或者鸿蒙
  static String appPlatform = "";

  /// 设备信息
  static String deviceModel = "";

  /// 当前用户信息
  static late LoginUserDataEntity curUser;

  // 底部弹框高度 - 用来处理多层fcBottomDialog,最上层要覆盖下层弹框
  static late double fcBottomDialogHeight;
  static late double fcBottomDialogBodyHeight;
  static String curBreakPoint = 'sm'; // 当前断点类型

  ///初始化全局信息，会在APP启动时执行
  Future init() async {
    prefs = await SPUtils.perInit();
    //获取当前用户信息
    var userString = prefs?.get<String>('user');
    print('userString=$userString');
    if (userString != null) {
      try {
        curUser = LoginUserDataEntity.fromJson(jsonDecode(userString));
      } catch (e) {
        print(e);
      }
    }
  }

  ///本地存储token
  static void saveToken(String? token) {
    prefs?.setString('token', token);
  }

  ///获取本地存储token
  static String getToken() {
    if (prefs?.get('token') != null) {
      return prefs?.get('token');
    } else
      return "";
  }

  ///本地存储当前用户信息
  static void saveUser(LoginUserDataEntity user) {
    curUser = user;
    prefs?.setString('user', jsonEncode(curUser.toJson()));
  }

  //清空本地用户信息
  static void clearUserInfo() {
    saveUser(LoginUserDataEntity());
    saveToken('');
  }

  ///获取平台信息
  static String getAppPlatform() {
    if (Platform.isIOS) {
      return "IOS";
    } else if (Platform.isAndroid) {
      return "ANDROID";
    }
    return "";
  }


  ///获取设备信息
  static getClientInfo() async {
    const JsonEncoder encoder = JsonEncoder();
    var data = {};
    if (Platform.isIOS) {
      data.putIfAbsent("clientPlatform", () => 0);
    }else if(Platform.isAndroid){
      data.putIfAbsent("clientPlatform", () => 1);
    }
    return encoder.convert(data);
  }

}
